package net.gxtvsgwt.client.demo.type;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.gxtvsgwt.client.demo.BaseDemo;

import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.dom.client.Style.Overflow;
import com.google.gwt.user.cellview.client.CellTree;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.TreeViewModel;

public class TreeDemo extends BaseDemo {

	@Override
	public Widget createGxt() {
		TreeStore<BaseModelData> treeStore = new TreeStore<BaseModelData>();
		Map<String, Object> rootMap = new HashMap<String, Object>();
		rootMap.put("name", "root");
		BaseModelData root = new BaseModelData(rootMap);
		treeStore.add(root, false);
		for (int i = 0; i < dataSize; i++) {
			Map<String, Object> parentmap = new HashMap<String, Object>();
			parentmap.put("name", "parent" + i);
			BaseModelData parent = new BaseModelData(parentmap);
			Map<String, Object> childmap = new HashMap<String, Object>();
			childmap.put("name", "child" + i);
			BaseModelData child = new BaseModelData(childmap);
			Map<String, Object> grandchildmap = new HashMap<String, Object>();
			grandchildmap.put("name", "grandchild" + i);
			BaseModelData grandchild = new BaseModelData(grandchildmap);
			treeStore.add(root, parent, false);
			treeStore.add(parent, child, false);
			treeStore.add(child, grandchild, false);
		}
		com.extjs.gxt.ui.client.widget.treepanel.TreePanel<BaseModelData> tree = new com.extjs.gxt.ui.client.widget.treepanel.TreePanel<BaseModelData>(
				treeStore);
		tree.setHeight("200px");
		tree.setWidth("300px");
		tree.setDisplayProperty("name");
		return tree;
	}

	@Override
	public Widget createGwt() {
		final ListDataProvider<String> treeStore = new ListDataProvider<String>();
		List<String> list = treeStore.getList();
		for (int i = 0; i < dataSize; i++) {
			list.add("parent" + i);
		}
		TreeViewModel treeViewModel = new TreeViewModel() {
			@Override
			public <T> NodeInfo<?> getNodeInfo(T value) {
				return new DefaultNodeInfo<String>(
						value == null ? new ListDataProvider<String>(Arrays
								.asList(new String[] { "root" }))
								: value.toString().startsWith("root") ? treeStore
										: value.toString().startsWith("child") ? new ListDataProvider<String>(
												Arrays.asList(new String[] { "grand"
														+ value.toString() }))
												: new ListDataProvider<String>(
														Arrays.asList(new String[] { value
																.toString()
																.replace(
																		"parent",
																		"child") })),
						new TextCell(), null, null);
			}

			@Override
			public boolean isLeaf(Object value) {
				return value != null && value.toString().startsWith("grand");
			}
		};
		CellTree tree = new CellTree(treeViewModel, null);
		tree.setWidth("300px");
		tree.setDefaultNodeSize(Integer.MAX_VALUE);
		SimplePanel panel = new SimplePanel();
		panel.add(tree);
		panel.setHeight("200px");
		panel.getElement().getStyle().setOverflow(Overflow.AUTO);
		return panel;
	}

	@Override
	public String getName() {
		return "Tree";
	}
}
